package com.dhcc.wb.operationlog;

import com.dhcc.common.database.DBManager;
import com.dhcc.common.util.StringUtil;
import org.apache.log4j.Logger;
import java.util.List;

/**
 * 操作日志Dao
 * @author wanggx
 * @date 2019/12/5 16:39
 */
public class OperationLogDao {
    private static Logger logger = Logger.getLogger(OperationLogDao.class);
    public Integer queryCount(String name,String typeName) {
        DBManager dbm = null;
        Integer count = null;
        try {
            dbm = new DBManager();
            String sql = "select count(*) from(SELECT shl.id id, (CASE shl.type WHEN 1 THEN '新闻' WHEN 2 THEN '通讯录' WHEN 3 THEN '工资查询' WHEN 4 THEN '发行数据' WHEN 5 THEN 'APP下载'"+
                    " WHEN 6 THEN '最新资讯' WHEN 7 THEN '轮播图' WHEN 8 THEN '首页' WHEN 9 THEN '我的' WHEN 10 THEN '最新资讯' WHEN 11 THEN '工资查询' WHEN 12 THEN '电子物品' WHEN 13 THEN '信息化周报' " +
                    "WHEN 14 THEN '调研报告' WHEN 15 THEN '公众号统计' WHEN 16 THEN '反馈意见' WHEN 17 THEN '集团选题审批' WHEN 18 THEN '人民社评分' WHEN 19 THEN '选题追踪'" +
                    " WHEN 20 THEN 'AI助手' WHEN 21 THEN '出版社选题审批' END) as result,"
                    + "sho.shuhai_userName FROM sh_log shl right JOIN sh_openid sho ON sho.openid = shl.userid) g where 1=1 and g.id is not null";
            if(!StringUtil.isNullOrEmpty(name)){
                sql += " and g.shuhai_userName like '%"+ name +"%'";
            }
            if(!StringUtil.isNullOrEmpty(typeName)){
                sql += " and g.result like '%"+ typeName +"%'";
            }
            logger.info("操作日志总数sql："+sql);
            count = dbm.executeQueryCount(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.close();
        }
        return count;
    }

    /*
     * 查询所有操作日志
     */
    public List<OperationLogModel> findSh_logServiceByPage(Integer pageNumber, Integer pageSize, String name, String typeName) {
        DBManager dbm = null;
        List<OperationLogModel> list = null;
        try {
            dbm = new DBManager();
            String sql = "select * from(SELECT shl.id AS id, from_unixtime( shl.createtime / 1000, '%Y-%m-%d %H:%i' ) AS createtime,(CASE shl.type WHEN 1 THEN '新闻'"
                    + " WHEN 2 THEN '通讯录' WHEN 3 THEN '工资查询' WHEN 4 THEN '发行数据' WHEN 5 THEN 'APP下载' WHEN 6 THEN '最新资讯' "
                    + " WHEN 7 THEN '轮播图' WHEN 8 THEN '首页' WHEN 9 THEN '我的' WHEN 10 THEN '最新资讯' WHEN 11 THEN '工资查询' WHEN 12 THEN '电子物品' WHEN 13 THEN '信息化周报'" +
                    " WHEN 14 THEN '调研报告' WHEN 15 THEN '公众号统计' WHEN 16 THEN '反馈意见' WHEN 17 THEN '集团选题审批' WHEN 18 THEN '人民社评分' WHEN 19 THEN '选题追踪'" +
                    " WHEN 20 THEN 'AI助手' WHEN 21 THEN '出版社选题审批' END) as result,"
                    + " shl.type AS type, sho.shuhai_userName AS userid, shl.content AS content FROM sh_log shl RIGHT JOIN sh_openid sho ON sho.openid = shl.userid "
                    + "ORDER BY shl.createtime DESC)g where 1=1 and g.id IS NOT null";
            if(!StringUtil.isNullOrEmpty(name)){
                sql += " and g.userid LIKE '%"+ name +"%'";
            }
            if(!StringUtil.isNullOrEmpty(typeName)){
                sql += " and g.result = '"+ typeName +"'";
            }
            sql +=  " limit " + pageNumber + ","+ pageSize +"";
            list = dbm.getObjectList(OperationLogModel.class, sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.close();
        }
        return list;
    }




}
